package com.homework09;

import java.io.*;

public class Test {
    public static void main(String[] args) {
        String filePath = "C:/example/from.txt";
        String toPath = "C:/example/to.txt";
        copyByIO(filePath, toPath);
    }
    private static void copyByIO(String sourceFile, String targetFile) {
        try (InputStream input = new FileInputStream(sourceFile);
             OutputStream output = new FileOutputStream(targetFile);){
            byte[] bytes = new byte[4];
            int i = 0, len;
            while((len = input.read(bytes)) != -1) {
                output.write(bytes, 0, len);
                i++;
            }
            System.out.println("基于IO，复制成功，读写循环次数为" + i + "次!");
        }catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
